package io.renren.modules.hospital.controller;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.renren.modules.hospital.entity.JdEntity;
import io.renren.modules.hospital.service.JdService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author starry
 * @email 2495646304@qq.com
 * @date 2023-11-15 15:54:22
 */
@RestController
@RequestMapping("hospital/jd")
@Api(value = "病历节点接口", tags = "病历节点接口")
public class JdController {
    @Autowired
    private JdService jdService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    //@RequiresPermissions("hospital:jd:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = jdService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{jdId}")
    @ApiOperation("信息")
    //@RequiresPermissions("hospital:jd:info")
    public R info(@PathVariable("jdId") Integer jdId) {
        JdEntity jd = jdService.getById(jdId);
        return R.ok().put("jd", jd);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    //@RequiresPermissions("hospital:jd:save")
    public R save(@RequestBody JdEntity jd) {
        jdService.save(jd);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    //@RequiresPermissions("hospital:jd:update")
    public R update(@RequestBody JdEntity jd) {
        jdService.updateById(jd);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    //@RequiresPermissions("hospital:jd:delete")
    public R delete(@RequestBody Integer[] jdIds) {
        jdService.removeByIds(Arrays.asList(jdIds));

        return R.ok();
    }

    @GetMapping("/medical_node")
    @ApiOperation("根据患者的病历编号寻找所有的病历信息节点")
    public R getMedicalNodeById(Integer blId) {
        List<JdEntity> list = jdService.getMedicalNodeById(blId);
        return R.ok().put("page", list);
    }

    /**
     * 保存节点信息，带图片
     */
    @PostMapping("/saveWithImg")
    @ApiOperation("病历图片上传")
    public R uploadSingleFile(
            @RequestParam(value = "file") MultipartFile file) {
        String dirs = "/usr/share/nginx/html/img/";
        //单文件保存
        String imgName = UUID.randomUUID() + file.getOriginalFilename();
        try {
            file.transferTo(new File(dirs + imgName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok("img/" + imgName);
    }

    /**
     * 图片的上传
     */
    @PostMapping("/saveImgs")
    @ApiOperation("多图片上传")
    public R uploadMultiFiles(
            @RequestParam(value = "files") List<MultipartFile> files) {

        String dirs = "/usr/share/nginx/html/img/";
        StringBuffer sbf = new StringBuffer();
        for (MultipartFile file : files) {
            //单文件保存
            String imgName = UUID.randomUUID() + file.getOriginalFilename();
            try {
                file.transferTo(new File(dirs + imgName));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            sbf.append("img/").append(imgName).append(";");
        }
        return R.ok(sbf.toString());
    }

}
